home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCollec / PROrdCol.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  12.7 KB  |  391 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PROrdCol.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef PRORDCOL_H
  13. #include "PROrdCol.h"
  14. #endif
  15.  
  16. #ifndef PRVALLNK_H
  17. #include "PRValLnk.h"
  18. #endif
  19.  
  20. #ifndef FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #include "FWODExce.h"
  25.  
  26. //========================================================================================
  27. // Runtime informations
  28. //========================================================================================
  29.  
  30. #ifdef FW_BUILD_MAC
  31. #pragma segment fwcollec
  32. #endif
  33.  
  34. //========================================================================================
  35. // Ordered Collection API
  36. //========================================================================================
  37.  
  38. //----------------------------------------------------------------------------------------
  39. // FW_PrivOrderedCollection_AddFirst
  40. //----------------------------------------------------------------------------------------
  41.  
  42. unsigned long SL_API FW_PrivOrderedCollection_Count(FW_HLinkedList collection)
  43. {
  44.     // No try block necessary - Do not throw
  45.     return collection->Count(); 
  46. }
  47.  
  48. //----------------------------------------------------------------------------------------
  49. //  FW_PrivOrderedCollection_AddFirst
  50. //----------------------------------------------------------------------------------------
  51.  
  52. FW_PlatformError SL_API FW_PrivOrderedCollection_AddFirst(FW_HLinkedList collection, 
  53.                                                         const void* element)
  54. {
  55.     FW_RETURN_ERR_TRY
  56.     {
  57.         FW_ASSERT(element != NULL);
  58.         
  59.         FW_CPrivValueLink* newLink = new FW_CPrivValueLink((void*)element);
  60.         
  61.         collection->AddFirst(newLink);
  62.     }
  63.     FW_RETURN_ERR_CATCH
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. //   FW_PrivOrderedCollection_AddLast
  68. //----------------------------------------------------------------------------------------
  69.  
  70. FW_PlatformError SL_API FW_PrivOrderedCollection_AddLast(FW_HLinkedList collection, 
  71.                                                         const void* element)
  72. {
  73.     FW_RETURN_ERR_TRY
  74.     {
  75.         FW_ASSERT(element != NULL);
  76.         
  77.         FW_CPrivValueLink* newLink = new FW_CPrivValueLink((void*)element);
  78.         collection->AddLast(newLink);
  79.     }
  80.     FW_RETURN_ERR_CATCH
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. //   FW_PrivOrderedCollection_AddBefore
  85. //----------------------------------------------------------------------------------------
  86.  
  87. FW_PlatformError SL_API FW_PrivOrderedCollection_AddBefore(FW_HLinkedList collection, 
  88.                                                         FW_OrderedCollection_MatchProc matchProc,
  89.                                                         const void* existing, 
  90.                                                         const void* tobeadded)
  91. {
  92.     FW_RETURN_ERR_TRY
  93.     {
  94.         FW_ASSERT(tobeadded != NULL);
  95.     
  96.         FW_CPrivLinkedListIterator iter(collection);
  97.         FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  98.         while (aLink != NULL)
  99.         {
  100.             void* v = ((FW_CPrivValueLink*) aLink)->GetValue();
  101.     
  102.             if ((matchProc)(v,existing))    
  103.             {
  104.                 FW_CPrivValueLink* newLink = new FW_CPrivValueLink((void*)tobeadded);
  105.                 collection->AddBefore(aLink, newLink);
  106.                 aLink = NULL;
  107.             }
  108.             else
  109.                 aLink = (FW_CPrivValueLink*) iter.Next();
  110.         }
  111.     }
  112.     FW_RETURN_ERR_CATCH
  113. }
  114.  
  115. //----------------------------------------------------------------------------------------
  116. //  FW_PrivOrderedCollection_AddAfter
  117. //----------------------------------------------------------------------------------------
  118.  
  119. FW_PlatformError SL_API FW_PrivOrderedCollection_AddAfter(FW_HLinkedList collection,
  120.                                                         FW_OrderedCollection_MatchProc matchProc,
  121.                                                         const void* existing, 
  122.                                                         const void* tobeadded)
  123. {
  124.     FW_RETURN_ERR_TRY
  125.     {
  126.         FW_ASSERT(tobeadded != NULL);
  127.     
  128.         FW_CPrivLinkedListIterator iter(collection);
  129.         FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  130.         while (aLink != NULL)
  131.         {
  132.             void* v = ((FW_CPrivValueLink*) aLink)->GetValue();
  133.     
  134.             if ((matchProc)(v,existing))    
  135.             {
  136.                 FW_CPrivValueLink* newLink = new FW_CPrivValueLink((void*)tobeadded);
  137.                 collection->AddAfter(aLink, newLink);
  138.                 aLink = NULL;
  139.             }
  140.             else
  141.                 aLink = (FW_CPrivValueLink*) iter.Next();
  142.         }
  143.     }
  144.     FW_RETURN_ERR_CATCH
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. //  FW_PrivOrderedCollection_After
  149. //----------------------------------------------------------------------------------------
  150.  
  151. void* SL_API FW_PrivOrderedCollection_After(FW_HLinkedList collection,
  152.                                         FW_OrderedCollection_MatchProc matchProc,
  153.                                         const void* existing)
  154. {
  155.     FW_CPrivValueLink* linkAfter = NULL;
  156.     
  157.     // No try block necessary - Do not throw
  158.     FW_CPrivLinkedListIterator iter(collection);
  159.  
  160.     for (FW_CPrivValueLink* link = (FW_CPrivValueLink*) iter.First(); iter.IsNotComplete(); link = (FW_CPrivValueLink*) iter.Next())
  161.     {
  162.         void* v = ((FW_CPrivValueLink*) link)->GetValue();
  163.  
  164.         if ((matchProc)(v,existing))    
  165.         {
  166.             linkAfter = (FW_CPrivValueLink*) collection->After(link);
  167.             break;
  168.         }
  169.     }
  170.  
  171.     return linkAfter ? linkAfter->GetValue() : NULL;
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. //  FW_PrivOrderedCollection_Before
  176. //----------------------------------------------------------------------------------------
  177.  
  178. void* SL_API FW_PrivOrderedCollection_Before(FW_HLinkedList collection,
  179.                                         FW_OrderedCollection_MatchProc matchProc,
  180.                                         const void* existing)
  181. {
  182.     // No try block necessary - Do not throw
  183.     FW_CPrivValueLink* linkBefore = NULL;
  184.  
  185.     FW_CPrivLinkedListIterator iter(collection);
  186.  
  187.     for (FW_CPrivValueLink* link = (FW_CPrivValueLink*) iter.First(); iter.IsNotComplete(); link = (FW_CPrivValueLink*) iter.Next())
  188.     {
  189.         void* v = ((FW_CPrivValueLink*) link)->GetValue();
  190.  
  191.         if ((matchProc)(v,existing))    
  192.         {
  193.             linkBefore = (FW_CPrivValueLink*) collection->Before(link);
  194.             break;
  195.         }
  196.     }
  197.     
  198.     return linkBefore ? linkBefore->GetValue() : NULL;
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. //  FW_PrivOrderedCollection_First
  203. //----------------------------------------------------------------------------------------
  204.  
  205. void* SL_API FW_PrivOrderedCollection_First(FW_HLinkedList collection)
  206. {
  207.     // No try block necessary - Do not throw
  208.     FW_CPrivValueLink* firstLink = (FW_CPrivValueLink*) collection->First();
  209.     return firstLink ? firstLink->GetValue() : NULL;
  210. }
  211.  
  212. //----------------------------------------------------------------------------------------
  213. //  FW_PrivOrderedCollection_Last
  214. //----------------------------------------------------------------------------------------
  215.  
  216. void* SL_API FW_PrivOrderedCollection_Last(FW_HLinkedList collection)
  217. {
  218.     // No try block necessary - Do not throw
  219.     FW_CPrivValueLink* lastLink = (FW_CPrivValueLink*)collection->Last();
  220.     return lastLink ? lastLink->GetValue() : NULL;
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. //  FW_PrivOrderedCollection_RemoveFirst
  225. //----------------------------------------------------------------------------------------
  226.  
  227. void* SL_API FW_PrivOrderedCollection_RemoveFirst(FW_HLinkedList collection)
  228. {
  229.     // No try block necessary - Do not throw
  230.     void* value = NULL;
  231.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) collection->RemoveFirst();
  232.     if (aLink != NULL)
  233.     {
  234.         value = aLink->GetValue();
  235.         delete aLink;
  236.     }
  237.     return value;
  238. }
  239.  
  240. //----------------------------------------------------------------------------------------
  241. //  FW_PrivOrderedCollection_RemoveLast
  242. //----------------------------------------------------------------------------------------
  243.  
  244. void* SL_API FW_PrivOrderedCollection_RemoveLast(FW_HLinkedList collection)
  245. {
  246.     // No try block necessary - Do not throw
  247.     void* value = NULL;
  248.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) collection->RemoveLast();
  249.     if (aLink != NULL)
  250.     {
  251.         value = aLink->GetValue();
  252.         delete aLink;
  253.     }
  254.     return value;
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. //  FW_PrivOrderedCollection_Remove
  259. //----------------------------------------------------------------------------------------
  260.  
  261. void SL_API FW_PrivOrderedCollection_Remove(FW_HLinkedList collection, 
  262.                                             FW_OrderedCollection_MatchProc matchProc, 
  263.                                             const void* existing)
  264. {
  265.     // No try block necessary - Do not throw
  266.     FW_CPrivLinkedListIterator iter(collection);
  267.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  268.     while (aLink != NULL)
  269.     {
  270.         void* v = ((FW_CPrivValueLink*) aLink)->GetValue();
  271.  
  272.         if ((matchProc)(v,existing))
  273.         {
  274.             collection->Remove(aLink);
  275.             delete aLink;
  276.             aLink = NULL;    
  277.         }
  278.         else
  279.             aLink = (FW_CPrivValueLink*) iter.Next();
  280.     }    
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. //  FW_PrivOrderedCollection_RemoveAll
  285. //----------------------------------------------------------------------------------------
  286.  
  287. void SL_API FW_PrivOrderedCollection_RemoveAll(FW_HLinkedList collection)
  288. {
  289.     // No try block necessary - Do not throw
  290.     FW_CPrivLink* link = collection->RemoveFirst();
  291.     while (link != NULL)
  292.     {
  293.         delete link;
  294.         link = collection->RemoveFirst();
  295.     }
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //  FW_PrivOrderedCollection_Contains
  300. //----------------------------------------------------------------------------------------
  301.  
  302. FW_Boolean SL_API FW_PrivOrderedCollection_Contains(FW_HLinkedList collection, 
  303.                                                 FW_OrderedCollection_MatchProc matchProc, 
  304.                                                 const void* existing)
  305. {
  306.     // No try block necessary - Do not throw
  307.     FW_CPrivLinkedListIterator iter(collection);
  308.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  309.     while (aLink != NULL)
  310.     {
  311.         void* v = ((FW_CPrivValueLink*) aLink)->GetValue();
  312.  
  313.         if ((matchProc)(v,existing))
  314.         {
  315.             return TRUE;    
  316.         }
  317.         else
  318.             aLink = (FW_CPrivValueLink*) iter.Next();
  319.     }    
  320.     return FALSE;
  321. }
  322.  
  323. //========================================================================================
  324. // OrderedCollection Iterator
  325. //========================================================================================
  326.  
  327. //----------------------------------------------------------------------------------------
  328. // FW_PrivOrderedCollectionIterator_First
  329. //----------------------------------------------------------------------------------------
  330.  
  331. void* SL_API FW_PrivOrderedCollectionIterator_First(FW_HLinkedListIterator iterator)
  332. {
  333.     // No try block necessary - Do not throw
  334.     FW_CPrivValueLink* link = (FW_CPrivValueLink*) iterator->First();
  335.     return link ? link->GetValue() : NULL;
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. // FW_PrivOrderedCollectionIterator_Next
  340. //----------------------------------------------------------------------------------------
  341.  
  342. void* SL_API FW_PrivOrderedCollectionIterator_Next(FW_HLinkedListIterator iterator)
  343. {        
  344.     // No try block necessary - Do not throw
  345.     FW_CPrivValueLink* link = (FW_CPrivValueLink*) iterator->Next();
  346.     return link ? link->GetValue() : NULL;
  347. }
  348.  
  349. //----------------------------------------------------------------------------------------
  350. // FW_PrivOrderedCollectionIterator_Last
  351. //----------------------------------------------------------------------------------------
  352.  
  353. void* SL_API FW_PrivOrderedCollectionIterator_Last(FW_HLinkedListIterator iterator)
  354. {
  355.     // No try block necessary - Do not throw
  356.     FW_CPrivValueLink* link = (FW_CPrivValueLink*) iterator->Last();
  357.     return link ? link->GetValue() : NULL;
  358. }
  359.  
  360. //----------------------------------------------------------------------------------------
  361. // FW_PrivOrderedCollectionIterator_Previous
  362. //----------------------------------------------------------------------------------------
  363.  
  364. void* SL_API FW_PrivOrderedCollectionIterator_Previous(FW_HLinkedListIterator iterator)
  365. {
  366.     // No try block necessary - Do not throw
  367.     FW_CPrivValueLink* link = (FW_CPrivValueLink*)iterator->Previous();
  368.     return link ? link->GetValue() : NULL;
  369. }
  370.  
  371. //----------------------------------------------------------------------------------------
  372. // FW_PrivOrderedCollectionIterator_Current
  373. //----------------------------------------------------------------------------------------
  374.  
  375. void* SL_API FW_PrivOrderedCollectionIterator_Current(FW_HLinkedListIterator iterator)
  376. {
  377.     // No try block necessary - Do not throw
  378.     FW_CPrivValueLink* link = (FW_CPrivValueLink*)iterator->Current();
  379.     return link ? link->GetValue() : NULL;
  380. }
  381.  
  382. //----------------------------------------------------------------------------------------
  383. // FW_PrivOrderedCollectionIterator_IsNotComplete
  384. //----------------------------------------------------------------------------------------
  385.  
  386. FW_Boolean SL_API FW_PrivOrderedCollectionIterator_IsNotComplete(FW_HLinkedListIterator iterator)
  387. {
  388.     // No try block necessary - Do not throw
  389.     return iterator->IsNotComplete();
  390. }
  391.